package net.sharpf2.api.util.web;

import java.io.*;
import java.net.*;

public class GEServerLookup
{

    public GEServerLookup(int ids[])
    {
        items = ids;
        itemName = new String[items.length];
        itemPrice = new int[items.length];
        itemDesc = new String[items.length];
        loadData();
    }

    private boolean loadData()
    {
        try{
            String data = (new StringBuilder()).append(URLEncoder.encode("item", "UTF-8")).append("=").append(URLEncoder.encode(implode(items, ","), "UTF-8")).toString();
            URL url = new URL("http://aoery.samsmekens.com/projects/acebot/GELookup/lookup.php");
            URLConnection conn = url.openConnection();
            conn.addRequestProperty("User-Agent", "acebot");
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String result;
            for(int i = 0; (result = rd.readLine()) != null; i++)
            {
                itemName[i] = result.split(";")[0];
                itemPrice[i] = Integer.parseInt(result.split(";")[1]);
                itemDesc[i] = result.split(";")[2];
            }
            return true;
        }catch(Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public int price(int i)
    {
        return itemPrice[i];
    }

    public String name(int i)
    {
        return itemName[i];
    }

    public String examine(int i)
    {
        return itemDesc[i];
    }

    private String implode(int o[], String d)
    {
        String r = "";
         for(int i=0; i<o.length; i++) {
            if(i!=0) { r += d; }
            r += o[i];
        }
        return r;
    }

    private int items[];
    private String itemName[];
    private int itemPrice[];
    private String itemDesc[];
}